package pos.MainMenu;

import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.media.j3d.*;
import javax.vecmath.*;
import pos.utils.*;

/**
 * 主菜单实体
 * @author plus
 */
public class MainMenu extends BranchGroup implements Invokeable,IContainer{
    private static ArrayList<Pickable> subItems;
    private static Dock2D modeChooser;
    private static TransformGroup TGBackground=null;
    private Pickable currentItem;
    private MainMenuController myController;

    /**
     * 构造函数
     * @param myController 主菜单实体的控制器
     */
    public MainMenu(MainMenuController myController) {
        this.myController = myController;
        this.setCapability(ALLOW_DETACH);
        if(TGBackground==null || subItems==null)
            initObjects();
        this.addChild(TGBackground);
        this.compile();
    }

    /**
     * 为条件调用器调用的方法
     * @param Step 条件调用器送回的步进值
     */
    public void invoke(double Step) {
        modeChooser.invoke(Step);
    }

    
    /**
     * 执行子对象发起的事件，不做处理的时间发回控制器处理
     * @param EventName 事件名。每个事件名绑定一个Command对象，对应的Command对象将被执行
     * @param Param 传递给对应Command的参数
     * @return 由对应Command的参数
     */
    public Object doEvent(String eventName, Object Param) {
        if (eventName.equals("SetHotItem")){
            refreshCurrentItem((Pickable)Param);
            return null;
        }
        else return myController.doEvent(eventName, Param);
    }

    /**
     * 处理键盘事件
     * @param evt 待处理的键盘事件
     */
    public void processKeyEvent(KeyEvent evt) {
        if (evt.getID()==KeyEvent.KEY_RELEASED){
            switch(evt.getKeyCode()){
                case KeyEvent.VK_UP:
                    preItem();
                    break;
                case KeyEvent.VK_DOWN:
                    nextItem();
                    break;
                case KeyEvent.VK_ENTER:
                    currentItem.setStatue(Pickable.Action);
                    break;
                default:
                    if (currentItem instanceof IContainer) 
                        ((IContainer)currentItem).processKeyEvent(evt);
            }
        }
    }
    /**
     * 添加子对象
     * @param item 需添加的子对象
     * @return 当前主菜单实体中的子对象个数
     */
    public int addSubItem(Pickable item) {
        item.setHost(this);
        subItems.add(item);
        this.addChild((TransformGroup)item);
        return subItems.size();
    }
    
    private void nextItem(){
        int nextItemID=(subItems.indexOf(currentItem)+1)%subItems.size();
        refreshCurrentItem(subItems.get(nextItemID));
    }
    private void preItem(){
        int nextItemID=(subItems.indexOf(currentItem)-1);
        if (nextItemID<0) nextItemID+=subItems.size();
        refreshCurrentItem(subItems.get(nextItemID));
    }
    
    private void refreshCurrentItem(Pickable currentItem){
        if(this.currentItem.equals(currentItem)) return;    //仅当变化发生时更新
        this.currentItem=currentItem;
        for(int itemID=0; itemID<subItems.size();itemID++){
            if(!subItems.get(itemID).equals(currentItem)){
                subItems.get(itemID).setStatue(Pickable.Idle);
            }else
                subItems.get(itemID).setStatue(Pickable.Hot);
        }
    }
    /**
     * 激发当前模式选择器发出进入模式命令，仅供Enter命令调用
     */
    void enterCurrentMode(){
        modeChooser.setStatue(Pickable.Action);
    }
    
    private void initObjects(){
        Transform3D t3dBackground=new Transform3D();                            //载入背景图片
        t3dBackground.set(new Vector3d(0,7,0));
        TGBackground= new TransformGroup(t3dBackground);
        TGBackground.addChild(ObjectFactory.createTexturedSurface(42f, 18f, -50f, new Color3f(1,1,1), 0f, PathRes.getPath("Image.MainMenu.Shh"),false,false,false));
        
        subItems=new ArrayList<Pickable>();
        addSubItem(new ImageButton2D(
                new Vector3d(0.3,-0.25,-1),0.18f,0.05f,"Image.MainMenu.Enter",
                new Color3f(0.8f,0.8f,0.8f),new Color3f(0.5f,0.5f,0.8f),new Color3f(0.5f,0.8f,0.5f),"Enter"));
        addSubItem(new ImageButton2D(
                new Vector3d(-0.3,-0.25,-1),0.18f,0.05f,"Image.MainMenu.Exit",
                new Color3f(0.3f,0.3f,0.3f),new Color3f(0.8f,0.8f,0.3f),new Color3f(0.8f,0.3f,0.3f),"ExitMsg"));
        
        modeChooser=new Dock2D(new Vector3d(-0.1,-0.15,-1),0.15f,0.15f,1.5f);
        modeChooser.addSubItem("Image.MainMenu.Drive", 
                new Color3f(0.8f,0.8f,0.8f), new Color3f(0.5f,0.5f,0.8f), new Color3f(0.4f,0.4f,0.9f), 
                "ChangeModeToDrive","驾驶任务");
        modeChooser.addSubItem("Image.MainMenu.Option", 
                new Color3f(0.8f,0.8f,0.8f), new Color3f(0.8f,0.8f,0.5f), new Color3f(0.9f,0.9f,0.4f), 
                "ChangeModeToConfig","系统设置");
        
        currentItem=modeChooser;
        addSubItem(modeChooser);
        currentItem.setStatue(Pickable.Hot);
    }
    
}
